<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="业务分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ywfl">
              <!--<a-input :disabled=formDisabled v-model="model.ywfl" placeholder="请输入业务分类"  ></a-input>-->
              <!--<j-dict-select-tag :disabled=formDisabled type="list" v-model="model.ywfl" dictCode="ywfx_type" placeholder="请选择业务分类" />-->

              <business-category-select
                  :disabled=formDisabled
                  v-model="model.ywfl" @back="businessCategoryHandle"/>

            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="业务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ywmc">
              <a-input :disabled=formDisabled v-model="model.ywmc" placeholder="请输入业务名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="合同占比(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="htzb">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" v-model="model.htzb" placeholder="请输入合同占比"
                              style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分配合同额（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fphte">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" v-model="model.fphte"
                              placeholder="请输入分配合同额" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生产团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sctd">
              <!--<a-input :disabled=formDisabled v-model="model.sctd" placeholder="请输入生产团队"  ></a-input>-->
              <select-team-input
                  :disabled=formDisabled
                  v-model="model.sctd"
                  :tdlx="2"
                  @change="sctdChange"
                  :triggerChange="true"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="团队负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tdfzr">
              <select-user-by-dep disabled :multi="false" v-model="model.tdfzr"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { getAction } from '@/api/manage'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import BusinessCategorySelect from '@views/erp/components/BusinessCategorySelect';

export default {
  name: 'ErpOmcContractManagerClassForm',
  components: {
    SelectTeamInput,
    SelectUserByDep,
    BusinessCategorySelect
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        ywfl: [
          { required: true, message: '请选择业务分类!' }
        ],
        ywmc: [
          { required: true, message: '请选择业务名称!' }
        ],
        htzb: [
          { required: true, message: '请选择合同占比(%)!' }
        ],
        fphte: [
          { required: true, message: '请选择分配合同额!' }
        ],
        sctd: [
          { required: true, message: '请选择生产团队!' }
        ],
        tdfzr: [
          { required: true, message: '请选择团队负责人!' }
        ]
      },
      url: {
        add: '/api/erp/omc/contractmanager/erpOmcContractManagerClass/add',
        edit: '/api/erp/omc/contractmanager/erpOmcContractManagerClass/edit',
        queryById: '/api/erp/omc/contractmanager/erpOmcContractManagerClass/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.$refs['form'].clearValidate();
      Object.assign(this.$data, this.$options.data())
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.model.id) {
            this.model.id = parseInt(Math.random() * 1000000000000);
          }
          that.$emit('ok', this.model);
        }

      })
    },
    businessCategoryHandle(obj) {
      if (obj) {
        this.model.ywfl = obj.categoryCode
        //categoryName
        this.model.ywmc = obj.categoryName;
      }
    },
    sctdChange(val) {
      //debugger;
      getAction('/api/erp/omc/teammanager/erpOmcTeamManager/queryById', { id: val }).then((res) => {
        if (res.success) {
          // debugger;
          let data = res.result.records || res.result
          this.$set(this.model, 'sctd', val)
          this.$set(this.model, 'tdfzr', data.tdfzr)
        }
      })
    }
  }
}
</script>
